import CryptoJS from 'crypto-js'
import request from '@/api'
const encryptKey = 'azEaijI7BD6pPigFHidEXn95'
const encryptIV = 'D076D35C'
var pubkeyHex = '04AF0FCC45059AA342221352E5268614F2FF7A430497B156C0DEE6E751AB44E4957E9E69299E2CD38E25985B7BD34E0E7BBA683DE4725A6A8CD07E19BFF8BEF44D'

// 3DES加密desEncrypt
export function encrypt3DES(str: string, key = encryptKey, iv = encryptIV) {
  let cryptoKey = CryptoJS.enc.Utf8.parse(key)
  var cryptoIv = CryptoJS.enc.Utf8.parse(iv.substring(0, 8))
  let encrypted = CryptoJS.TripleDES.encrypt(str, cryptoKey, {
    iv: cryptoIv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  })
  // 这里的encrypted不是字符串，而是一个CipherParams对象
  return encrypted.ciphertext.toString(CryptoJS.enc.Base64)
}

// 3DES解密desDecrypt
export function decrypt3DES(str: string, key = encryptKey, iv = encryptIV) {
  let cryptoKey = CryptoJS.enc.Utf8.parse(key)
  let cryptoIv = CryptoJS.enc.Utf8.parse(iv)
  // 返回的是一个Word Array Object，其实就是Java里的字节数组
  let decrypted = CryptoJS.TripleDES.decrypt(str, cryptoKey, {
    iv: cryptoIv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  })
  return decrypted.toString(CryptoJS.enc.Utf8)

}
